#include <QtGui>
#include <QtSql>

#include <QDebug>

#include "regionSearch.h"
#include "resultItem.h"
#include "database_connection.h"

RegionSearch::RegionSearch(QWidget *parent)
    : QDialog(parent)
{
    resize(800, 480);
    isEmpty = 0;
    pageSeq = 1;

    area = new QComboBox;
    ridgepole = new QComboBox;
    layer = new QComboBox;
    room = new QComboBox;
    devName = new QComboBox;

    areaLabel = new QLabel("区", this);
    ridgepoleLabel = new QLabel("栋", this);
    layerLabel = new QLabel("层", this);
    roomLabel = new QLabel("房", this);
    devNameLabel = new QLabel("设备", this);
    tmpLabel = new QLabel("未找到结果", this);

    pageUp = new QPushButton("上一页", this);
    pageDown = new QPushButton("下一页", this);

    groupBox = new QGroupBox(this);

    QFont tmpFont = tmpLabel->font();
    tmpFont.setPointSize(100);
    tmpLabel->setFont(tmpFont);
    tmpLabel->setAlignment(Qt::AlignCenter);

    //area->addItem(" ");
    area->addItem("A");
    area->addItem("B");
    //ridgepole->addItem(" ");
    ridgepole->addItem("001");
    ridgepole->addItem("002");
    ridgepole->addItem("003");
    ridgepole->addItem("004");
    ridgepole->addItem("005");
    //layer->addItem(" ");
    layer->addItem("001");
    layer->addItem("002");
    layer->addItem("003");
    layer->addItem("005");
    room->addItem(" ");
    room->addItem("101");
    room->addItem("211");
    room->addItem("222");
    room->addItem("311");
    devName->addItem(" ");
    devName->addItem("消防水泵阀");
    devName->addItem("排烟阀");

    connect(area, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(ridgepole, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(layer, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(room, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(devName, SIGNAL(currentIndexChanged(int)), this, SLOT(updateResult()));
    connect(pageUp, SIGNAL(clicked()), this, SLOT(pageSeqDes()));
    connect(pageDown, SIGNAL(clicked()), this, SLOT(pageSeqInc()));

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(area);
    topLayout->addWidget(areaLabel);
    topLayout->addWidget(ridgepole);
    topLayout->addWidget(ridgepoleLabel);
    topLayout->addWidget(layer);
    topLayout->addWidget(layerLabel);
    topLayout->addWidget(room);
    topLayout->addWidget(roomLabel);
    topLayout->addWidget(devName);
    topLayout->addWidget(devNameLabel);
    topLayout->addStretch();
    topLayout->addWidget(pageUp);
    topLayout->addWidget(pageDown);

    bottomLayout = new QGridLayout;

    mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(tmpLabel);
    //mainLayout->addStretch();

    setLayout(mainLayout);
    setWindowTitle("区域查询");

    updateResult();
}

void RegionSearch::updateResult()
{
    delete bottomLayout;
    numOfItem = 0;
    QSqlDatabase::database().transaction();
    QSqlQuery query;

    QString selectQuery = " ";
    QString areaTemp = area->currentText() + QString("区");
    QString ridgepoleTemp = ridgepole->currentText() + "栋";
    QString layerTemp = layer->currentText() + "层";
    QString roomTemp = room->currentText() + "房";
    QString devNameTemp = devName->currentText();

    if(area->currentText() != " ")
    {
        selectQuery += QString("district = '%1' ").arg(areaTemp);
    }
    if(ridgepole->currentText() != " ")
    {
        selectQuery += QString("and building = '%1' ").arg(ridgepoleTemp);
    }
    if(layer->currentText() != " ")
    {
        selectQuery += QString("and layer = '%1' ").arg(layerTemp);
    }
    if(room->currentText() != " ")
    {
        selectQuery += QString("and room = '%1' ").arg(roomTemp);
    }
    if(devName->currentText() != " ")
    {
        selectQuery += QString("and device_name = '%1' ").arg(devNameTemp);
    }

    query.exec(QString("select id from locate_information_table where %1").arg(selectQuery));
    qDebug() << QString("select id from locate_information_table where %1").arg(selectQuery);
    while(query.next())
    {
        numOfItem++;
    }

    maxPages = numOfItem / numOfPerPage + 1;
    qDebug() << maxPages;
    int i = 0;
    query.exec(QString("select id, district, building, layer, room, device_name from"
                       "(select * from locate_information_table where %1) limit %2, %3").arg(selectQuery).arg((pageSeq-1)*20).arg(numOfPerPage));
    while(query.next())
    {
        item[i] = new ResultItem(this);
        item[i]->setNum(query.value(0).toString());
        areaTemp = query.value(1).toString();
        ridgepoleTemp = query.value(2).toString();
        layerTemp = query.value(3).toString();
        roomTemp = query.value(4).toString();
        devNameTemp = query.value(5).toString();

        QString pos = areaTemp + ridgepoleTemp + layerTemp + roomTemp;
        item[i]->setPos(pos);
        item[i]->setLogo(devNameTemp);
        i++;
    }

    count = i;
    bottomLayout = new QGridLayout;
    for(i=0; i<count; i++)
    {
        bottomLayout->addWidget(item[i], i/5, i%5);
    }

    if(count < numOfPerPage)
    {
        for(; i<numOfPerPage; i++)
        {
            item[i] = new ResultItem;
            bottomLayout->addWidget(item[i], i/5, i%5);
        }
    }

    if(i != 0)
    {
        if(isEmpty == 0)
        {
            tmpLabel->hide();
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        else
        {
            tmpLabel->hide();
            mainLayout->removeWidget(groupBox);
            groupBox->close();
            groupBox = new QGroupBox(this);
            groupBox->setLayout(bottomLayout);
            mainLayout->addWidget(groupBox);
        }
        isEmpty = 1;
    }
    else
    {
        groupBox->close();
        tmpLabel->show();

    }
    QSqlDatabase::database().commit();

}

void RegionSearch::pageSeqInc()
{
    if(pageSeq == maxPages)
    {
        qDebug() << "最后一页";
    }
    else
    {
        pageSeq++;
        updateResult();
    }
}

void RegionSearch::pageSeqDes()
{
    if(pageSeq == 1)
    {
        qDebug() << "已到首页";
    }
    else
    {
        pageSeq--;
        updateResult();
    }
}
